/* This file is part of the Crimsoon project
  * Copyright (C) 2009 Eduardo Henrique <eduardohleite@gmail.com>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
  * License as published by the Free Software Foundation; either
  * version 2 of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public License
  * along with this library; see the file COPYING.LIB.  If not, write to
  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  * Boston, MA 02110-1301, USA.
*/

#include <QLabel>
#include <QVBoxLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QFileDialog>
#include <QCheckBox>
#include <QMessageBox>
#include <QTextStream>

#include "importmediadialog.h"
#include "gallery.h"
#include "scanner.h"

void ImportMediaDialog::createWidgets()
{
    detailLabel   = new QLabel("Select the folder you wish to scan for media files");
    layout        = new QVBoxLayout;
    browseLayout  = new QHBoxLayout;
    folderEdit    = new QLineEdit;
    browseButton  = new QPushButton;
    scanSubdir    = new QCheckBox;
    buttonsLayout = new QHBoxLayout;
    okButton      = new QPushButton("&Ok", this);
    cancelButton  = new QPushButton("&Cancel", this);
    statusLabel   = new QLabel;

    setFixedWidth(450);

    browseButton->setText("&Browse...");
    connect(browseButton, SIGNAL(clicked()), this, SLOT(onBrowseButtonClick()));
    browseLayout->addWidget(folderEdit);
    browseLayout->addWidget(browseButton);

    scanSubdir->setText("&Also scan subdirectories");
    scanSubdir->setChecked(true);

    connect(okButton, SIGNAL(clicked()), this, SLOT(onOkButtonClick()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));

    statusLabel->setText(getGalleryStatus());

    buttonsLayout->addSpacerItem(new QSpacerItem(200,0,QSizePolicy::Fixed, QSizePolicy::Fixed));
    buttonsLayout->addWidget(cancelButton);
    buttonsLayout->addWidget(okButton);

    layout->addWidget(detailLabel);
    layout->addLayout(browseLayout);
    layout->addWidget(scanSubdir);
    layout->addWidget(statusLabel);
    layout->addLayout(buttonsLayout);

    setLayout(layout);
}

QString ImportMediaDialog::getGalleryStatus()
{
    QString message;
    Gallery gallery;

    QTextStream (&message) << "There are currently " << gallery.getTotalFiles() << " files in the gallery.";

    return message;
}

void ImportMediaDialog::disableForm()
{
    detailLabel ->setEnabled(false);
    folderEdit  ->setEnabled(false);
    browseButton->setEnabled(false);
    scanSubdir  ->setEnabled(false);
    okButton    ->setEnabled(false);
    cancelButton->setEnabled(false);
}

void ImportMediaDialog::onBrowseButtonClick()
{
    QString path;
    path = QFileDialog::getExistingDirectory(this, tr("Scan directory"), "", QFileDialog::ShowDirsOnly);
    if (!path.isEmpty())
    {
        folderEdit->setText(path);
    }
}

void ImportMediaDialog::onOkButtonClick()
{
    if (folderEdit->text().isEmpty())
    {
        QMessageBox::information(this, tr("Please select folder"),
            tr("Please select the folder you wish to add to the gallery"), QMessageBox::Ok);
    }

    QDir dir;

    if (!dir.exists(folderEdit->text()))
    {
        QMessageBox::information(this, tr("Please select folder"),
            tr("The selected folder does not exist. Please select the folder you wish to add to the gallery"), QMessageBox::Ok);

        folderEdit->setText("");

        return;
    }

    disableForm();
    statusLabel->setText("Scanning...");

    filesCount = 0;

    scanner = new Scanner(this, folderEdit->text(), scanSubdir->isChecked());
    connect(scanner, SIGNAL(finished()), this, SLOT(onScanEnd()));
    connect(scanner, SIGNAL(musicFileFound(MusicFile)), this, SLOT(onMusicFileFound(MusicFile)));
    connect(scanner, SIGNAL(scanningDirChanged(QString)), this, SLOT(onScanDirectoryChanged(QString)));
    scanner->start();
}

void ImportMediaDialog::onScanEnd()
{
    QString message;
    QTextStream (&message) << "Scan completed. " << filesCount << " files were added.";

    statusLabel->setText("Done.");

    QMessageBox::information(this, tr("Scan completed"), message, QMessageBox::Ok);

    accept();
}

void ImportMediaDialog::onScanDirectoryChanged(QString path)
{
    QString message;
    QTextStream (&message) << "Scanning directory " << path;
    statusLabel->setText(message);
}

void ImportMediaDialog::onMusicFileFound(MusicFile)
{
    filesCount++;
}

ImportMediaDialog::ImportMediaDialog(QWidget *parent)
        :QDialog(parent)
{
    createWidgets();
    scanner = NULL;
}

ImportMediaDialog::~ImportMediaDialog()
{
    delete detailLabel;
    delete scanSubdir;
    delete okButton;
    delete cancelButton;
    delete folderEdit;
    delete browseButton;
    delete buttonsLayout;
    delete browseLayout;
    delete layout;

    if (scanner != NULL)
    {
        delete scanner;
        scanner = NULL;
    }
}
